/**
 * Created by Administrator on 2017/9/22.
 */
module.exports=function(grunt){
    grunt.initConfig({
        pkg:grunt.file.readJSON('package.json'),
        //压缩js
        uglify:{
            options:{
                stripBanners:true,
                banner:'/*!<%=pkg.name%>-<%=pkg.version%>.min.js*/',
                mangle: true, //混淆变量名
                comments: 'false' //false（删除全部注释），some（保留@preserve @license @cc_on等注释）
            },
            my_target: {
                files: [{
                    expand:true,
                    cwd:'xscp/js/', //js目录下
                    src:['*.js'], //所有js文件
                    dest:'xscp/js/'  //输出到此目录下
                }]
            }
        },
        //检查js语法错误
        jshint:{
            build:['Gruntfile.js','src/*.js'],
            options: {
                jshintrc: '.jshintrc'
            }
        },
        //实时监测文件改动
        watch:{
            build:{
                files:['cp/js/*.js','cp/css/*.css','cp/*.html'],
                tasks:['copy','imagemin','cssmin','htmlmin','babel','jshint','uglify'],
                options:{spawn:false}
            },
            babel:{
                files:'cp/js/*.js',
                tasks:['babel']
            }
        },
        //把文件从开发环境(cp)复制到线上环境(xscp)
        copy: {
            main: {
                expand: true,
                cwd: 'cp/',
                src: '**',
                dest: 'xscp/',
            },
        },
        //先把es6转化为es5
        babel: {
            options: {
                sourceMap: false,
                presets: ['babel-preset-es2015']

            },
            dist: {
                files: [{
                    expand:true,
                    cwd:'xscp/js/', //js目录下
                    src:['*.js'], //所有js文件
                    dest:'xscp/js/'  //输出到此目录下
                }]
            }
        },
        //压错图片
        imagemin: {
            /* 压缩图片大小 */
            dist: {
                options: {
                    optimizationLevel: 3 //定义 PNG 图片优化水平
                },
                files: [
                    {
                        expand: true,
                        cwd: 'xscp/cssimg/',
                        src: ['**/*.{png,jpg,jpeg}'], // 优化 img 目录下所有 png/jpg/jpeg 图片
                        dest: 'xscp/cssimg/' // 优化后的图片保存位置，覆盖旧图片，并且不作提示
                    }
                ]
            }
        },
        //压错css
        cssmin:{
            yasuo:{
                options:{
                    mangle:false
                },
                expand: true,
                cwd: 'xscp/css/',//压缩那个文件夹里的文件
                src:'*.css',//压缩那个文件
                dest:'xscp/css/',//放压缩后文件的文件夹
            }
        },
        //压缩html
        htmlmin:{
            options: {
                removeComments: true, //移除注释
                removeCommentsFromCDATA: true,//移除来自字符数据的注释
                collapseWhitespace: true,//无用空格
                collapseBooleanAttributes: true,//失败的布尔属性
                removeAttributeQuotes: false,//移除属性引号      有些属性不可移走引号
                removeRedundantAttributes: false,//移除多余的属性
                useShortDoctype: true,//使用短的跟元素
                removeEmptyAttributes: false,//移除空的属性
                removeOptionalTags: true//移除可选附加标签
            },
            yasuo:{
                expand: true,
                cwd: 'xscp/',
                src: ['*.html'],
                dest: 'xscp/'
            }
        }
});
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-babel');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-imagemin');
    grunt.loadNpmTasks('grunt-contrib-htmlmin');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.registerTask('default',['copy','imagemin','cssmin','htmlmin','babel','jshint','uglify','watch']);
};
